package com.yiyibook.controller;

import com.yiyibook.entity.Book;
import com.yiyibook.entity.BookBigStyle;
import com.yiyibook.entity.BookSmallStyle;
import com.yiyibook.mapper.BookSmallStyleMapper;
import com.yiyibook.service.BookService;
import com.yiyibook.service.BookSmallStyleService;
import com.yiyibook.util.FieldErrorMsg;
import com.yiyibook.util.Msg;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 图书小类 Controller
 *
 * @author yi.wu
 * @date 2019-10-14
 */
@RestController
@RequestMapping("/small")
public class BookSmallStyleController {

    @Autowired
    private BookSmallStyleService bookSmallStyleService;

    @Autowired
    private BookSmallStyleMapper bookSmallStyleMapper;

    @Autowired
    private BookService bookService;

    @GetMapping("/getAll")
    public Msg getAllByBigStyleId(@RequestParam(defaultValue = "1")Integer bookBigStyleId){
        return Msg.success().add("bookSmallList",bookSmallStyleService.getAllByBigStyleId(bookBigStyleId));
    }

    @PostMapping("/insert")
    public Msg insertBigStyle(@Valid BookSmallStyle bookSmallStyle, BindingResult result){
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorMsg", FieldErrorMsg.errorMsg(result));
        }else{
            List<BookSmallStyle> bookSmallStyles = bookSmallStyleMapper.selectSmallStyleByCondition(bookSmallStyle);
            if(CollectionUtils.isNotEmpty(bookSmallStyles)){
                return Msg.fail().add("errorMsg","图书大类名称已存在，请重新输入！");
            }else{
                int i = bookSmallStyleService.insertSelective(bookSmallStyle);
                if(i>0){
                    return Msg.success().add("successMsg","添加成功");
                }else{
                    return Msg.fail().add("errorMsg","网络波动，请重试");
                }
            }
        }
    }

    @GetMapping("/selectOne")
    public BookSmallStyle selectOneById(Integer smallStyleId){
        return bookSmallStyleMapper.selectByPrimaryKey(smallStyleId);
    }

    @PostMapping("/update")
    public Msg update(@Valid BookSmallStyle bookSmallStyle, BindingResult result){
        Integer smallStyleId = bookSmallStyle.getSmallStyleId();
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorMsg", FieldErrorMsg.errorMsg(result));
        }else{
            bookSmallStyle.setSmallStyleId(null);
            List<BookSmallStyle> bookSmallStyles = bookSmallStyleMapper.selectSmallStyleByCondition(bookSmallStyle);
            if(CollectionUtils.isNotEmpty(bookSmallStyles)){
                BookSmallStyle bookSmallStyle1 = bookSmallStyles.get(0);
                return Msg.fail().add("errorMsg","图书大类" + bookSmallStyle1.getBigStyleName() + "下，图书小类" + bookSmallStyle1.getSmallStyleName() + "已存在，请重新输入！");
            }else{
                bookSmallStyle.setSmallStyleId(smallStyleId);
                int i = bookSmallStyleService.updateByPrimaryKeySelective(bookSmallStyle);
                if(i>0){
                    return Msg.success().add("successMsg","修改成功");
                }else{
                    return Msg.fail().add("errorMsg","网络波动，请重试");
                }
            }
        }
    }

    @GetMapping("/delete")
    public Msg delete(Integer smallStyleId){
        List<Book> books = bookService.selectByBookSmallStyleId(smallStyleId);
        if(CollectionUtils.isNotEmpty(books)){
            return Msg.fail().add("errorMsg","该图书小类下还有图书，请先删除图书！");
        }else{
            int i = bookSmallStyleService.deleteByPrimaryKey(smallStyleId);
            if(i>0){
                return Msg.success().add("successMsg","删除成功");
            }else{
                return Msg.fail().add("errorMsg","网络波动，请重试");
            }
        }
    }

    @GetMapping("/deleteAny")
    public Msg deleteAnySmallStyleByIds(String smallStyleIds){
        // 保存 无法被删除的 图书小类类 （即图书小类下还存在图书的情况）
        String smallStyleName = "";
        // 记录 是否删除成功
        int j = 0;
        String[] split = smallStyleIds.split(", ");
        Integer[] smallStyleIdsArr = new Integer[split.length];
        for (int i = 0; i < smallStyleIdsArr.length; i++) {
            smallStyleIdsArr[i] = Integer.parseInt(split[i]);
        }
        for (int i = 0; i < smallStyleIdsArr.length; i++) {
            List<Book> books = bookService.selectByBookSmallStyleId(smallStyleIdsArr[i]);
            if(CollectionUtils.isNotEmpty(books)){
                BookSmallStyle bookSmallStyle = bookSmallStyleMapper.selectByPrimaryKey(smallStyleIdsArr[i]);
                if(i == smallStyleIdsArr.length-1){
                    smallStyleName = smallStyleName + bookSmallStyle.getSmallStyleName();
                }else{
                    smallStyleName = smallStyleName + bookSmallStyle.getSmallStyleName() + ",";
                }
            }else{
               j = bookSmallStyleService.deleteByPrimaryKey(smallStyleIdsArr[i]);
            }
        }
        if(j > 0){
            return Msg.success().add("successMsg","删除成功");
        }else if(j == 0){
            return Msg.fail().add("errorMsg","以下 " + smallStyleName + " 图书小类下还有图书，无法删除，请先删除图书!");
        }else {
            return Msg.fail().add("errorMsg","网络波动，请重试");
        }
    }

    @GetMapping("/selectAllByBigStyleId")
    public Msg selectAllByBigStyleId(Integer bigStyleId){
        List<BookSmallStyle> bookSmallStyles = bookSmallStyleMapper.selectByBookBigStyleId(bigStyleId);
        return Msg.success().add("smallStyleList", bookSmallStyles);
    }
}